package com.cy;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cy.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }

    @Bean
    @LoadBalanced
    public RestTemplate loadBalanceRestTemplate(){
        return new RestTemplate();
    }
    @RestController
    public class ConsumerController{
        @Autowired
        private LoadBalancerClient loadBalancerClient;

        @Autowired
        private RestTemplate restTemplate;

        @Autowired
        private RestTemplate loadBalanceRestTemplate;

        @Autowired
        private ConsumerService consumerService;

        @GetMapping("/consumer/findById/{id}")
        @SentinelResource("res")
        public String doFindById(@PathVariable Integer id){
            return "resource id is "+id;
        }


        private AtomicLong aLong = new AtomicLong(1);

        @Value("${spring.application.name}")
        private String appName;
        @GetMapping("/consumer/doRestEcho1")
        public String doRestEcho1() throws InterruptedException {
            long num = aLong.getAndIncrement();
            if(num%2==0){
                Thread.sleep(200);//
            }

            consumerService.doGetResource();
            String url = "http://localhost:8082/provider/echo/"+appName;
            return restTemplate.getForObject(url,String.class);
        }
        @GetMapping("/consumer/doRestEcho2")
        public String doRestEcho2(){
            consumerService.doGetResource();
            ServiceInstance instance = loadBalancerClient.choose("sca-provider");
            String ip = instance.getHost();//ip
            int port = instance.getPort();//端口号
            //String url = "http://"+ip+":"+port+"/provider/echo/"+appName;
            String url = String.format("http://%s:%s/provider/echo/%s",ip,port,appName);
            return restTemplate.getForObject(url,String.class);
        }
        @GetMapping("/consumer/doRestEcho3")
        public String doRestEcho3(){
            String url = String.format("http://sca-provider/provider/echo/%s", appName);
            return loadBalanceRestTemplate.getForObject(url,String.class);
        }
    }
}
